package com.hschinese.pronunciation.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.interfaces.LessonProgressInterface;
import com.hschinese.pronunciation.interfaces.NextLessonInterface;
import com.hschinese.pronunciation.interfaces.RecordStatusListener;
import com.hschinese.pronunciation.interfaces.VolumeListener;
import com.hschinese.pronunciation.utils.Logger;
import com.hschinese.pronunciation.utils.NumberFormatUtil;
import com.hschinese.pronunciation.widget.GestureListener;
import com.hschinese.pronunciation.widget.MyPopupWindow;
import com.hschinese.pronunciation.widget.StudyChinapracticeView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.hschinese.pronunciation.R.id.study_progress;

public class StudayChinesePracticeActivity extends BaseActivity {
    @Bind(R.id.study_quit)
    ImageView studyQuit;
    @Bind(study_progress)
    ProgressBar studyProgress;
    @Bind(R.id.study_progress_layout)
    LinearLayout studyProgressLayout;
    @Bind(R.id.study_container)
    LinearLayout studyContainer;
    @Bind(R.id.tv_process)
    TextView tvProcess;
    @Bind(R.id.study_main_view)
    RelativeLayout studyMainView;
    @Bind(R.id.study_header_dividing_line)
    View mBaseView;

    private StudyChinapracticeView mStudyWordsView;


    // 0,2学习，1,3 练习
    private int mCurrentLesson = 0;
    private int position = 0;
    private NextLessonInterface mNextListener;
    private MainGestrueListener mGestureListener;
    private boolean isStarted = true;
    private RecordStatusListener mRecordListener;
    private VolumeListener mVolumeListener;
    private MyPopupWindow mPopupWindow;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_studay_recognition_content);
        ButterKnife.bind(this);
        Intent it = getIntent();

        // 启动出决定开始的是哪个课程
        mCurrentLesson = it.getIntExtra("type", 0);
        Logger.e("xionghy-currentLesson: " + mCurrentLesson);
        setNextListener();
        mGestureListener = new StudayChinesePracticeActivity.MainGestrueListener(StudayChinesePracticeActivity.this);
        initViews();

    }
    private void initRecordListener() {
        mRecordListener = new RecordStatusListener() {
            @Override
            public void startRecord() {
                Logger.e("xionghy - study activity start record");
                mPopupWindow.setVolumeText(0.f);
                mPopupWindow.showPopupWindow(mBaseView);
            }

            @Override
            public void stopRecord() {
                Logger.e("xionghy - study activity stop record");
                mPopupWindow.dismiss();
            }
        };
    }
    private void initViews() {
        initRecordListener();
        initVolumeListener();
        studyMainView.setLongClickable(true);
        studyMainView.setOnTouchListener(mGestureListener);
        mPopupWindow = new MyPopupWindow(this);
        if (mCurrentLesson == 1 || mCurrentLesson == 3) {

        } else if (mCurrentLesson == 15 ) {
            initStudyData();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mStudyWordsView != null) {
            mStudyWordsView.onDestroy();
        }

    }
    private void initVolumeListener() {
        mVolumeListener = new VolumeListener() {
            @Override
            public void updateVolume(float volume) {
                if (mPopupWindow != null) {
                    mPopupWindow.setVolumeText(volume);
                }
            }
        };
    }
    private void initStudyData() {
        mStudyWordsView = new StudyChinapracticeView(StudayChinesePracticeActivity.this);
        mStudyWordsView.setNextListener(mNextListener);
        mStudyWordsView.setLessonType(mCurrentLesson);
        studyContainer.addView(mStudyWordsView);
        mStudyWordsView.setCurrentPosition(position);
        mStudyWordsView.setVolumeListener(mVolumeListener);
        mStudyWordsView.setRecordListener(mRecordListener);
        mStudyWordsView.setListener(new LessonProgressInterface() {
            @Override
            public void setProgress(int total, int position) {
                Logger.e("setProgress -- total: " + total + " --  position:" + position);
                studyProgress.setMax(total);
                studyProgress.setProgress(position + 1);
                tvProcess.setText(NumberFormatUtil.getPercent(position, total) + "%");
            }
        });
        mStudyWordsView.initData();
    }


    private void setNextListener() {
        mNextListener = new NextLessonInterface() {
            @Override
            public void onLessonChanged(int currentLesson) {
                mCurrentLesson = currentLesson;
                // if (mCurrentLesson <= 0) return;
               /* if (currentLesson == 2) {
                    if (mPractiseView != null) {
                        mPractiseView.onDestroy();
                        mContainerView.removeView(mPractiseView);
                    }
                    initStudyData();
                } else if (currentLesson == 1 || currentLesson == 3) {
                    if (mStudyWordsView != null) {
                        mStudyWordsView.onDestroy();
                        mContainerView.removeView(mStudyWordsView);

                    }

                } else if (currentLesson == 9 || currentLesson == 8) {
                    if (mDictationPracticeView != null) {
                        mDictationPracticeView.onDestroy();
                        mContainerView.removeView(mDictationPracticeView);
                    }

                } else if (currentLesson == 7) {
                    if (mKnowledgePointView != null) {
                        mKnowledgePointView.onDestroy();
                        mContainerView.removeView(mKnowledgePointView);
                    }

                } else if (currentLesson == 4) {
                    Intent it = new Intent(StudyConstructionActivity.this, WordListActivity.class);
                    it.putExtra("lessonType", currentLesson);
                    startActivity(it);
                    finish();
                } else if (currentLesson == 5) {
                    Intent it = new Intent(StudyConstructionActivity.this, ConsListActivity.class);
                    it.putExtra("lessonType", currentLesson);
                    startActivity(it);
                    finish();
                } else if (currentLesson == 6) {
                    if (mConsonantStudyView != null) {
                        mConsonantStudyView.onDestroy();
                        mContainerView.removeView(mConsonantStudyView);
                    }

                } else if (currentLesson >= 7) {
                    // 结束当前Activity，进入关卡页
                    finish();
                }*/
            }
        };
    }


    @Override
    public void onBackPressed() {
        this.finish();
        if (mStudyWordsView != null) {
            mStudyWordsView.onDestroy();
        }

    }

    private void gotoNextWord() {
        if (mCurrentLesson == 15) {
            mStudyWordsView.nextWord();
        }
    }



    @OnClick({R.id.study_quit, R.id.tv_process})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.study_quit:
                this.finish();
                break;

            case R.id.tv_process:
                break;
        }
    }


    class MainGestrueListener extends GestureListener {

        public MainGestrueListener(Context context) {
            super(context);
        }

        @Override
        public boolean left() {
            gotoNextWord();
            return true;
        }

        @Override
        public boolean right() {

            mStudyWordsView.beforeWord();

            return true;
        }
    }

}
